
//tofixed方法为四舍五入取整
var len=0;
var db = wx.cloud.database();
const _ = db.command;
var i = (Math.random() * 899 + 100).toFixed(0);
var hc = wx.getStorageSync('name')
var value = wx.getStorageSync('user')
// i = (Math.random() * 899 + 100).toFixed(0)
//i的取值范围为[10000,99999)的小数
Page({

  /**
   * 页面的初始数据
   */
  data: {
    number:i,
    length:"0",
    canuseinfo:"邀请码可用未知",
    canues:false,
    datasize:"1",
    bfclass:[],
    teacherid:"",
    openid:value.openid
  },
 

  create:function(e){
  
  //创建班级
  // console.log(e.detail.value.year)
  wx.showLoading({
    title: '数据加载中',
    mask:true
  })
 
   var sign=0;
   var year=e.detail.value.year;
   var class0=e.detail.value.class0;
   var classname=year+class0+"";
   var id=this.data.teacherid;
   console.log("year",year,"class",class0)

   if(this.data.canues==true){
    if(year!="" && class0!=""){
      
      console.log("teachername",hc)
      console.log("classname",classname)
      //一切准备就绪开始数据库操作
      
      console.log("datasize",this.data.datasize)
      if(this.data.datasize=="0"){
        wx.cloud.callFunction({
          name:"add",
          data:{
            table:'teacher',
            datas:{
              data:{
                teachername:hc,
                classes:[classname],
                openid: value.openid
              }
            }
          }
        }).then(res=>{
          console.log("第一次创建班级成功")
        })
     
        wx.cloud.callFunction({
          name:"add",
          data:{
            table:'class',
            datas:{
              data:{
                    classname:classname,
                    teachername:hc,
                    number:i,
                    students:[],
                    openid: value.openid
              }
            }
          }
        }).then(res=>{
          console.log("班级表中班级更新成功")
         
        wx.cloud.callFunction({
                name: "add",
                data: {
                    table: "Number",
                    datas: {
                        data: {
                            number: this.data.number + ""
                        }
                    }
                }
            }).then(res=>{
              wx.hideLoading(),
               wx.reLaunch({
                url: "../student/indexstudent"
              });
            })
        })
      }else{
        this.data.bfclass.forEach(function(item){
          if(item==classname){
            wx.showToast({
              title:"您有该名称班级",
              image:"../../images/warn.png"
            })
            sign = 1;
          }
        });
          if(sign == 0){
            console.log(id)
           
            wx.cloud.callFunction({
              name:"update",
              data:{
                table:"teacher",
                docid:this.data.teacherid,
                shux:"classes",
                classname:classname
                }
              
            }).then(res=>{
              console.log("老师表中班级更新成功")
              console.log("udate",res)
              wx.cloud.callFunction({
                name:"add",
                data:{
                  table:'class',
                  datas:{
                    data:{
                          classname:classname,
                          teachername:hc,
                          number:i,
                          students:[],
                          openid: value.openid
                    }
                  }
                }
              }).then(res=>{
                console.log("班级表中班级更新成功")
                wx.cloud.callFunction({
                  name: "add",
                  data: {
                      table: "Number",
                      datas: {
                          data: {
                              number: this.data.number + ""
                          }
                      }
                  }
              }).then(res=>{
                wx.hideLoading(),
                 wx.reLaunch({
                  url: "../student/indexstudent"
                });
              })
              })
            })
          
            
          }
        
      }

    
    }else{
    wx.showToast({
      title:"信息没填全呢",
      image:"../../images/warn.png"
    })
   }
  }else{
    wx.showToast({
      title:"邀请码貌似不可用呢",
      image:"../../images/warn.png"
    })

    
  }
  },
  //重置邀请码
  cnki:function(){
    i = (Math.random() * 899 + 100).toFixed(0);
    db.collection("Number").where({
      number:_.eq(i)
    }).get().then(res=>{
       console.log(res.data)
      len=res.data.length
      // console.log(res.data.length)
      // console.log(len==1)
      console.log(i)
      console.log(len)
      if(len==1){
        this.setData({
          number:i,
          canuseinfo:"当前邀请码不可用×",
          canues:false
        })
      }else{
        console.log('不重复的邀请码为')
        this.setData({
          number:i,
          canuseinfo:"当前邀请码可用√",
          canues:true
        })
      } 
    })
  },
  cxnumber:function(){
   this.cnki()
      
      
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    //第一次加载时判断老师情况
    db.collection("teacher").where({
      openid:value.openid
    }).get().then(res=>{
      console.log("dbteacher",res)
      if(res.data.length=="0"){
        this.setData({
          datasize:"0"
        })
      }else{
        this.setData({
          teacherid:res.data[0]._id,
          bfclass:res.data[0].classes
        })
      }
      console.log("bfclass",this.data.bfclass)
      console.log("datasize",this.data.datasize)
    })
    //第一次加载时判断邀请码是否可用
    db.collection("Number").where({
      number:_.eq(i)
    }).get().then(res=>{
       console.log(res.data)
       if(res.data.length==0){
         this.setData({
          canuseinfo:"当前邀请码可用√",
          canues:true
         })
       }else{
         this.setData({
          canuseinfo:"当前邀请码不可用×",
          canues:false
        })
       }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
     
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
